﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO.WaiGaoQiao;
using System.Data;
using System.ComponentModel;

namespace Model.WaiGaoQiao
{

    public class Work_ArticleBringInfo
    {
        private DataAccessWork_ArticleBring workBring;

        public Work_ArticleBringInfo()
        {
            workBring = new DataAccessWork_ArticleBring();
        }
        /// <summary>
        /// ID
        /// </summary>
        public int ID { get; set; }
        /// <summary>
        /// 物品名称
        /// </summary>
        public int Article_ID { get; set; }
        /// <summary>
        /// 物品名称
        /// </summary>
        public string Article_Unit { get; set; }
        /// <summary>
        /// 领取数量
        /// </summary>
        public int BringCount { get; set; }
        /// <summary>
        /// 领取日期
        /// </summary>
        public DateTime BringTime { get; set; }
        /// <summary>
        /// 用户工号
        /// </summary>
        public string PersonNo { get; set; }
        /// <summary>
        /// 用户姓名
        /// </summary>
        public string PersonName { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }
        /// <summary>
        /// 岗位id
        /// </summary>
        public int postid { get; set; }
        /// <summary>
        /// 领取的月份
        /// </summary>
        public string clu { get; set; }

        /// <summary>
        /// 获取要领取的物品
        /// </summary>
        /// <param name="personNo"></param>
        /// <param name="personName"></param>
        /// <returns></returns>
        public IList<Work_ArticleBringInfo> GetBringUser(string personNo, string personName)
        {
            IList<Work_ArticleBringInfo> dmlist = new List<Work_ArticleBringInfo>();
            Work_ArticleBringInfo dminfo;
            DataSet ds = workBring.GetBringUser(personNo, personName);
            foreach (DataRow item in ds.Tables[0].Rows)
            {
                dminfo = new Work_ArticleBringInfo();
                {
                    dminfo.ID = Convert.ToInt32(item[0]);
                    dminfo.Article_ID = Convert.ToInt32(item[1].ToString());
                    dminfo.postid = Convert.ToInt32(item[2].ToString());
                    dminfo.Article_Unit = Convert.ToString(item[3].ToString());
                    dminfo.BringCount = Convert.ToInt32(item[4].ToString());
                    dminfo.PersonNo = RtnPersonNo(item[5].ToString());
                    dminfo.PersonName = item[6].ToString();
                    dminfo.clu = item[7].ToString();
                }
                dmlist.Add(dminfo);

            }
            return dmlist;
        }
        /// <summary>
        /// 根据条件查询领取的物资
        /// </summary>
        /// <returns></returns>
        public IList<Work_ArticleBringInfo> GetAllWork_ArticleBring(string condition, int pagesize, int startindex)
        {
            IList<Work_ArticleBringInfo> dmlist = new List<Work_ArticleBringInfo>();
            Work_ArticleBringInfo dminfo;
            string rtn = string.Empty;
            if (!string.IsNullOrEmpty(condition))
            {

                string[] query = condition.Split(',');
                if (!string.IsNullOrEmpty(query[2]))
                {
                    dmlist = GetWork_ArticleBringByCardNo(query[2], query);
                }
                else
                {
                    if (!string.IsNullOrEmpty(query[0]))
                    {
                        rtn = " And PersonNo='" + RtnPidForPersonNo(query[0]) + "'";
                    }
                    if (!string.IsNullOrEmpty(query[1]))
                    {
                        rtn += " And PersonName like '%" + query[1] + "%'";
                    }
                    if (!string.IsNullOrEmpty(query[3]) && query[3].Equals(query[4]))
                    {
                        rtn += " And CONVERT(varchar,BringTime, 23 )='" + query[3] + "'";
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(query[3]))
                        {
                            rtn += " And CONVERT(varchar,BringTime, 23 )>='" + query[3] + "'";
                        }
                        if (!string.IsNullOrEmpty(query[4]))
                        {
                            rtn += " And CONVERT(varchar,BringTime, 23 )<='" + query[4] + "'";
                        }
                    }//else

                }
            }
                    DataSet ds = workBring.GetAllWork_ArticleBring(rtn, pagesize, startindex);
                    foreach (DataRow item in ds.Tables[0].Rows)
                    {
                        dminfo = new Work_ArticleBringInfo();
                        {
                            dminfo.ID = Convert.ToInt32(item[0]);
                            dminfo.Article_ID = Convert.ToInt32(item[1].ToString());
                            dminfo.Article_Unit = Convert.ToString(item[2].ToString());
                            dminfo.BringCount = Convert.ToInt32(item[3].ToString());
                            dminfo.BringTime = Convert.ToDateTime(item[4].ToString());
                            dminfo.PersonNo = RtnPersonNo(item[5].ToString());
                            dminfo.PersonName = item[6].ToString();
                            dminfo.Remark = item[7].ToString();
                        }
                        dmlist.Add(dminfo);

                    }

              
            
            return dmlist.OrderBy(m=>m.ID).ToList();
        }
        /// <summary>
        /// 根据卡号返回领用物资的集合
        /// </summary>
        /// <param name="cardNo"></param>
        /// <returns></returns>
        public IList<Work_ArticleBringInfo> GetWork_ArticleBringByCardNo(string cardNo, string[] query)
        {
            string rtn = string.Empty;
            IList<Work_ArticleBringInfo> dmlist = GetBringUser(cardNo, "");
            List<Work_ArticleBringInfo> rtnList = new List<Work_ArticleBringInfo>();
            Work_ArticleBringInfo wc = new Work_ArticleBringInfo();
            if (!string.IsNullOrEmpty(query[0]))
            {
                rtn = " And PersonNo='" + RtnPidForPersonNo(query[0]) + "'";
            }
            if (!string.IsNullOrEmpty(query[1]))
            {
                rtn += " And PersonName like '%" + query[1] + "%'";
            }
            if (!string.IsNullOrEmpty(query[3]) && query[3].Equals(query[4]))
            {
                rtn += " And CONVERT(varchar,BringTime, 23 )='" + query[3] + "'";
            }
            else
            {
                if (!string.IsNullOrEmpty(query[3]))
                {
                    rtn += " And CONVERT(varchar,BringTime, 23 )>='" + query[3] + "'";
                }
                if (!string.IsNullOrEmpty(query[4]))
                {
                    rtn += " And CONVERT(varchar,BringTime, 23 )<='" + query[4] + "'";
                }
            }//else

            foreach (Work_ArticleBringInfo wa in dmlist)
            {

                wc = GetWork_ArticleBring("" + rtn + " AND personno='" + RtnPidForPersonNo(wa.PersonNo) + "' AND  article_id='" + wa.Article_ID + "' AND Article_unit='" + wa.Article_Unit + "' And postId='" + wa.postid + "'");
                if (wc != null)
                    rtnList.Add(wc);
            }
            return rtnList;
        }

        /// <summary>
        /// 根据条件查询领取的物资的对象
        /// </summary>
        /// <returns></returns>
        public Work_ArticleBringInfo GetWork_ArticleBring(string condition)
        {

            Work_ArticleBringInfo dminfo = null;
            DataSet ds = workBring.GetAllWork_ArticleBring(condition);
            foreach (DataRow item in ds.Tables[0].Rows)
            {
                dminfo = new Work_ArticleBringInfo();
                {
                    dminfo.ID = Convert.ToInt32(item[0]);
                    dminfo.Article_ID = Convert.ToInt32(item[1].ToString());
                    dminfo.Article_Unit = Convert.ToString(item[2].ToString());
                    dminfo.BringCount = Convert.ToInt32(item[3].ToString());
                    dminfo.BringTime = Convert.ToDateTime(item[4].ToString());
                    dminfo.PersonNo = RtnPersonNo(item[5].ToString());
                    dminfo.PersonName = item[6].ToString();
                    dminfo.Remark = item[7].ToString();
                }
            }
            return dminfo;
        }
        /// <summary>
        /// 根据条件查询领取的物资的对象
        /// </summary>
        /// <returns></returns>
        public IList<Work_ArticleBringInfo> GetAllWork_ArticleBringList(string condition)
        {
            IList<Work_ArticleBringInfo> dmlist = new List<Work_ArticleBringInfo>();
            Work_ArticleBringInfo dminfo;
            string rtn = string.Empty;
            if (!string.IsNullOrEmpty(condition))
            {

                string[] query = condition.Split(',');
                if (!string.IsNullOrEmpty(query[2]))
                {
                    dmlist = GetWork_ArticleBringByCardNo(query[2], query);
                }
                else
                {
                    if (!string.IsNullOrEmpty(query[0]))
                    {
                        rtn = " And PersonNo='" + RtnPidForPersonNo(query[0]) + "'";
                    }
                    if (!string.IsNullOrEmpty(query[1]))
                    {
                        rtn += " And PersonName like '%" + query[1] + "%'";
                    }
                    if (!string.IsNullOrEmpty(query[3]) && query[3].Equals(query[4]))
                    {
                        rtn += " And CONVERT(varchar,BringTime, 23 )='" + query[3] + "'";
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(query[3]))
                        {
                            rtn += " And CONVERT(varchar,BringTime, 23 )>='" + query[3] + "'";
                        }
                        if (!string.IsNullOrEmpty(query[4]))
                        {
                            rtn += " And CONVERT(varchar,BringTime, 23 )<='" + query[4] + "'";
                        }
                    }//else

                 
                    DataSet ds = workBring.GetAllWork_ArticleBring(rtn);
                    foreach (DataRow item in ds.Tables[0].Rows)
                    {
                        dminfo = new Work_ArticleBringInfo();
                        {
                            dminfo.ID = Convert.ToInt32(item[0]);
                            dminfo.Article_ID = Convert.ToInt32(item[1].ToString());
                            dminfo.Article_Unit = Convert.ToString(item[2].ToString());
                            dminfo.BringCount = Convert.ToInt32(item[3].ToString());
                            dminfo.BringTime = Convert.ToDateTime(item[4].ToString());
                            dminfo.PersonNo = RtnPersonNo(item[5].ToString());
                            dminfo.PersonName = item[6].ToString();
                            dminfo.Remark = item[7].ToString();
                        }
                        dmlist.Add(dminfo);
                    }
                }
            }
            return dmlist;
        }
        /// <summary>
        /// 根据条件获取最大领取日期
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>

        public DateTime GetMaxBringTime(string condition)
        {
            return workBring.GetMaxBringTime(condition);
        }
        /// <summary>
        /// 获取卡的注册日期
        /// </summary>
        /// <returns></returns>
        public DateTime GetRegTime(string condition)
        {
            return workBring.GetRegTime(condition);
        }
        /// <summary>
        /// 保存或修改领取的物资
        /// </summary>
        /// <param name="dmpara"></param>
        /// <returns></returns>
        public bool SaveWork_ArticleBring(Work_ArticleBringInfo bring)
        {
            object[] dpmpara = new object[] {bring.Article_ID, bring.BringCount,bring.BringTime,bring.PersonNo,
                                              bring.PersonName,bring.Remark,bring.postid,bring.clu,bring.Article_Unit };
            return workBring.SaveWork_ArticleBring(dpmpara) > 0 ? true : false; ;
        }
        /// <summary>
        /// 根据条件获取所有劳资物品数量
        /// </summary>
        /// <returns></returns>
        public int GetWork_ArticleBringCount(string condition)
        {
            string rtn = string.Empty;
            int rtnCount = 0;
            if (!string.IsNullOrEmpty(condition))
            {

                string[] query = condition.Split(',');
                if (!string.IsNullOrEmpty(query[2]))
                {
                    rtnCount = GetWork_ArticleBringByCardNo(query[2], query).Count;
                }
                else
                {
                    if (!string.IsNullOrEmpty(query[0]))
                    {
                        rtn = " And PersonNo='" + RtnPidForPersonNo(query[0]) + "'";
                    }
                    if (!string.IsNullOrEmpty(query[1]))
                    {
                        rtn += " And PersonName like '%" + query[1] + "%'";
                    }
                    if (!string.IsNullOrEmpty(query[3]) && query[3].Equals(query[4]))
                    {
                        rtn += " And CONVERT(varchar,BringTime, 23 )='" + query[3] + "'";
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(query[3]))
                        {
                            rtn += " And CONVERT(varchar,BringTime, 23 )>='" + query[3] + "'";
                        }
                        if (!string.IsNullOrEmpty(query[4]))
                        {
                            rtn += " And CONVERT(varchar,BringTime, 23 )<='" + query[4] + "'";
                        }
                    }//else 
                    rtnCount = workBring.GetWork_ArticleBringCount(rtn);
                }


            }
            return rtnCount;

        }
        /// <summary>
        /// 返回序列号
        /// </summary>
        /// <param name="personNo"></param>
        /// <param name="personName"></param>
        /// <returns></returns>
        public string RtnSerial(string personNo)
        {
            return workBring.RtnSerial(personNo);
        }
        /// <summary>
        /// 返回personNo
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public string RtnPersonNo(string pid)
        {
            return workBring.RtnPersonNo(pid);
        }
        /// <summary>
        /// 返回用户姓名
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public string RtnPersonName(string personNo)
        {
            return workBring.RtnPersonName(personNo);
        }
        /// <summary>
        /// 返回pid
        /// </summary>
        /// <param name="personNo"></param>
        /// <param name="personName"></param>
        /// <returns></returns>
        public int RtnPid(string personNo)
        {
            return workBring.RtnPid(personNo);
        }
        /// <summary>
        /// 返回pid
        /// </summary>
        /// <param name="personNo"></param>
        /// <param name="personName"></param>
        /// <returns></returns>
        public int RtnPidForPersonNo(string personNo)
        {
            return workBring.RtnPidForPersonNo(personNo);
        }
        /// <summary>
        /// 返回岗位编号
        /// </summary>
        /// <param name="personNo"></param>
        /// <param name="personName"></param>
        /// <returns></returns>
        public int RtnPostNoForPersonNo(string personNo)
        {
            return workBring.RtnPostNoForPersonNo(personNo);
        }
    }
}
